package com.lq.leetcode;

import java.util.*;

/**
 * Main49
 *
 * @author qingqing
 * @date 2025/2/28
 */

public class Main49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            byte[] bytes = strs[i].getBytes();
            Arrays.sort(bytes);
            String key = new String(bytes);
            if (!map.containsKey(key)) {
                LinkedList<String> list = new LinkedList<String>();
                list.add(strs[i]);
                map.put(key, list);
            } else {
                List<String> list = map.get(key);
                list.add(strs[i]);
                map.put(key, list);
            }
        }
        return new LinkedList<List<String>>(map.values());
    }
}
